package icursoft.educationms.ui.user;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import icursoft.educationms.BaseActivity;
import icursoft.educationms.EducationMSApplication;
import icursoft.educationms.R;
import icursoft.educationms.bean.MTalkResultBO;
import icursoft.educationms.bean.StudentInfoBO;
import icursoft.educationms.eventbus.EventExit;
import icursoft.educationms.http.HttpCallBack;
import icursoft.educationms.http.HttpResult;
import icursoft.educationms.servive.PreferencesService;
import icursoft.educationms.util.CommUtil;
import icursoft.educationms.util.IDCardValidate;
import icursoft.educationms.util.MachTalkUtil;
import icursoft.educationms.view.CustomDialog;
import icursoft.educationms.view.NiceSpinner;
import icursoft.educationms.view.WheelView;

public class ImpPerInfActivity extends BaseActivity {

    private TextView mTitle, mRight;
    EditText mName, mStuNum, mPhone, mIdentifier,mSecondPhone,mQQ,mEmail,
            mSchool, mMajor;
    TextView mSex,mNation,mEnterYear,mIsArt,mProvince,mSignStatus,mEnglishLevel;
    private List<String> mSextaset,mNationtaset,mEnterYeartaset,mIsArttaset,mProvincetaset,mSignStatustaset,mEnglishLeveltaset;
    String mStrSextaset,mStrNation,mStrEnterYear,mStrIsArt,mStrProvince,mStrSignStatus,mStrEnglishLevel;
    StudentInfoBO mStudentInfoBO;

    protected void setupViews() {
        mTitle = (TextView) findViewById(R.id.title_tv);
        mTitle.setText(R.string.text_edit_info);
        mRight = (TextView) findViewById(R.id.right_tv);
        mRight.setText(R.string.submit_text);
        findViewById(R.id.left_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mName = getView(R.id.et_family_name);
        mStuNum = getView(R.id.et_title_info_stu_num);
        mStuNum.setFocusable(false);
        mStuNum.setFocusableInTouchMode(false);
        mPhone = getView(R.id.et_stu_phone);
        mIdentifier = getView(R.id.et_identifier);
        mSecondPhone = getView(R.id.et_second_phone);
        mQQ = getView(R.id.et_qq);
        mEmail = getView(R.id.et_email);
        mSchool =  getView(R.id.et_school);
        mMajor =  getView(R.id.et_major);

        mSex =  getView(R.id.nice_sex);
        mSex.setOnClickListener(this);
        mNation =  getView(R.id.nice_nation);
        mNation.setOnClickListener(this);
        mEnterYear =  getView(R.id.nice_enter_year);
        mEnterYear.setOnClickListener(this);
        mIsArt =  getView(R.id.nice_is_art);
        mIsArt.setOnClickListener(this);
        mProvince =  getView(R.id.nice_province);
        mProvince.setOnClickListener(this);
        mSignStatus =  getView(R.id.nice_sign_status);
        mEnglishLevel =  getView(R.id.nice_english_level);

        getView(R.id.right_layout).setOnClickListener(this);

    }

    @Override
    protected void initialized() {
        MachTalkUtil.getInstance(this).getSelfInfo(this,getSelfInfo, PreferencesService.getInstance(mContext).getUserPhone(),PreferencesService.getInstance(mContext).getTokenLong());
    }

//    @Override
//    public void onTokenShortSuccessLogicAgain() {
//        MachTalkUtil.getInstance(mContext).editStudentInfoBO(callback, info);
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_imp_per_inf;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_layout:
                if (TextUtils.isEmpty(mName.getText().toString())) {
                    CommUtil.showToast(mContext, R.string.text_student_info_1, Toast.LENGTH_SHORT);
                    return;
                }

                mStudentInfoBO = new StudentInfoBO();
                mStudentInfoBO.setUser_name(PreferencesService.getInstance(mContext).getUserPhone());
                mStudentInfoBO.setReal_name(mName.getText().toString());
                if(CommUtil.judgePhoneNums(this,mPhone.getText().toString())){
                    mStudentInfoBO.setPhone(mPhone.getText().toString());
                }else {
                    CommUtil.showToast(mContext, R.string.text_student_info_2, Toast.LENGTH_SHORT);
                }
               // mStudentInfoBO.setIdentifier(mIdentifier.getText().toString());
                String boolea = "1";
                try {
                    boolea= IDCardValidate.IDCardValidate(mIdentifier.getText().toString().trim());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if(("").equals(boolea)){
                    mStudentInfoBO.setIdentifier(mIdentifier.getText().toString());
                }else {
                    CommUtil.showToast(mContext, boolea, Toast.LENGTH_SHORT);
                    return;
                }
                mStudentInfoBO.setEmail(mEmail.getText().toString());
                if(mSex.getText().toString().equals("男")){
                    mStudentInfoBO.setSex(1);
                }else if(mSex.getText().toString().equals("女")){
                    mStudentInfoBO.setSex(2);
                }else {
                    mStudentInfoBO.setSex(0);
                }
                mStudentInfoBO.setPro_phone(mSecondPhone.getText().toString());
                mStudentInfoBO.setQq(mQQ.getText().toString());
                mStudentInfoBO.setSchool(mSchool.getText().toString());
                mStudentInfoBO.setMajor(mMajor.getText().toString());
                mStudentInfoBO.setNation(mNation.getText().toString());
                mStudentInfoBO.setProvince(mProvince.getText().toString());
                mStudentInfoBO.setEnter_year(mEnterYear.getText().toString());
                if(mIsArt.getText().toString().equals("是")){
                    mStudentInfoBO.setIs_art(1);
                }else if(mIsArt.getText().toString().equals("否")){
                    mStudentInfoBO.setIs_art(2);
                }else {
                    mStudentInfoBO.setIs_art(0);
                }

               //mStudentInfoBO.setStu_num(mStuNum.getText().toString());
               // mStudentInfoBO.setSign_status(mSignStatus.getText().toString());
                mStudentInfoBO.setEnglish_level(mEnglishLevel.getText().toString());
                MachTalkUtil.getInstance(this).editStudentInfo(this,editStudentInfo, mStudentInfoBO);
                break;
            case R.id.nice_sex:
                selectSex(mSex,mSextaset,R.array.gendar);
                break;
            case R.id.nice_nation:
                selectSex(mNation,mNationtaset,R.array.nation);
                break;
            case R.id.nice_enter_year:
                selectSex(mEnterYear,mEnterYeartaset,R.array.enterYear);
                break;
            case R.id.nice_is_art:
                selectSex(mIsArt,mIsArttaset,R.array.isart);
                break;
            case R.id.nice_province:
                selectSex(mProvince,mProvincetaset,R.array.province);
                break;
            case R.id.nice_sign_status:
                selectSex(mSignStatus,mSignStatustaset,R.array.signstatus);
                break;
            case R.id.nice_english_level:
                selectSex(mEnglishLevel,mEnglishLeveltaset,R.array.englishlevel);
                break;
        }
    }

    String mStr;
    /**
     * 选择职称
     */
    public void selectSex(final TextView textView, List<String> taset, int arr) {

        final CustomDialog dialog = new CustomDialog(this,
                CustomDialog.default_info_edit_width,
                CustomDialog.default_info_edit_height,
                R.layout.dialog_chose, R.style.myDialog);
        dialog.show();
        ((TextView) dialog.findViewById(R.id.dialog_title)).setText("");

        WheelView view = (WheelView) dialog.findViewById(R.id.chose_wheel);

        taset = new ArrayList<>();
        for (String s : getResources().getStringArray(arr)) {
            taset.add(s);
        }
        view.setItems(taset);
        view.setSeletion(0);
        mStr  = taset.get(0);
        //num  = 1;
        view.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                mStr = item;
                //num = selectedIndex;
            }
        });

        dialog.findViewById(R.id.passive_btn).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        dialog.dismiss();
                    }
                });
        dialog.findViewById(R.id.positive_btn).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        textView.setText(mStr);
                        dialog.dismiss();
                    }
                });
    }




    StringCallback editStudentInfo = new StringCallback() {
        @Override
        public void onSuccess(String str, okhttp3.Call call, okhttp3.Response response) {
            //上传成功
            Log.e("wangshu", str);
            HttpResult param = EducationMSApplication.GSON.fromJson(str , HttpResult.class);
            if (param.getMsg() >= 0) {
//            startActivity(InticareDActivity.class);
//            overridePendingTransition(R.anim.fade, R.anim.hold);

            }
            CommUtil.showToast(mContext, param.getMsgbox().toString(),  Toast.LENGTH_SHORT);

        }
        @Override
        public void upProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
            //这里回调上传进度(该回调在主线程,可以直接更新ui)
        }
    };
    StringCallback getSelfInfo = new StringCallback() {
        @Override
        public void onSuccess(String str, okhttp3.Call call, okhttp3.Response response) {
            //上传成功
            Log.e("wangshu", "getSelfInfo===="+str);
            HttpResult httpResult = EducationMSApplication.GSON.fromJson(str , HttpResult.class);
            if (httpResult.getMsg() >= 0) {
                MTalkResultBO param = EducationMSApplication.GSON.fromJson(str , MTalkResultBO.class);
                StudentInfoBO studentInfo = param.getData();
                if(!TextUtils.isEmpty(studentInfo.getReal_name()))
                mName.setText( studentInfo.getReal_name());
                mPhone.setText(studentInfo.getPhone());
                mIdentifier.setText(studentInfo.getIdentifier());
                mStuNum.setText(studentInfo.getStu_num());
                mSecondPhone.setText(studentInfo.getPro_phone());
                mQQ.setText(studentInfo.getQq());
                mEmail.setText(studentInfo.getEmail());
                mSchool.setText(studentInfo.getSchool());
                mMajor.setText(studentInfo.getMajor());
                mNation.setText(studentInfo.getNation());
                mEnterYear.setText(studentInfo.getEnter_year());
                if(studentInfo.getIs_art()==1){
                    mIsArt.setText("是");
                }else if(studentInfo.getIs_art()==2){
                    mIsArt.setText("否");
                }else {
                    mIsArt.setText("");
                }
                mProvince.setText(studentInfo.getProvince());
                mEnglishLevel.setText(studentInfo.getEnglish_level());
                if(studentInfo.getSex()==1){
                    mSex.setText("男");
                }else if(studentInfo.getSex()==2){
                    mSex.setText("女");
                }else {
                    mSex.setText("");
                }
                if(studentInfo.getSign_status()==1){
                    mSignStatus.setText("报名");
                }else if(studentInfo.getSign_status()==2){
                    mSignStatus.setText("未报名");
                }else {
                    mSignStatus.setText("");
                }

//            startActivity(InticareDActivity.class);
//            overridePendingTransition(R.anim.fade, R.anim.hold);
                CommUtil.showToast(mContext, httpResult.getMsgbox().toString(),  Toast.LENGTH_SHORT);
            }else {
                CommUtil.showToast(mContext, httpResult.getMsgbox().toString(),  Toast.LENGTH_SHORT);
                startActivity(LoginActivity.class);
                finish();
            }
        }
        @Override
        public void upProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
            //这里回调上传进度(该回调在主线程,可以直接更新ui)
        }
    };
}
